<template>
	<div class="printData-container">

		<PrintPreview ref="preViewRef" />
	</div>
</template>
<style scoped>
	:deep(.el-select),
	:deep(.el-input-number) {
		width: 100%;
	}
</style>
<script lang="ts" setup>
	import { ref, onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import request from '/@/utils/request';
	import PrintPreview from '/@/views/system/print/component/hiprint/preview.vue';
	import { hiprint } from 'vue-plugin-hiprint';
	//父级传递来的参数
	var props = defineProps({
		title: {
			type: String,
			default: "",
		},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	let mianPageData = ref<any>([]);
	let hiprintTemplate = ref();
	const editPrintRef = ref<InstanceType<typeof EditPrint>>();
	/*
	*    templateName:模版名称
	*    printType :  0:打印预览后打印   1：直接打印
	*    queryData: 需要打印的数据Id集合
	*/
	const commentClick = async (templateName : any, printType : number, queryData : []) => {
		var printData = await getPrintData(templateName, queryData);
		await goPrint(templateName, printType, printData);
		// if (templateName == '物流追踪卡') {
		// 	let printDataLessThan7 = []; //小于7个
		// 	let printDataGreaterThanOREqual7 = []; //大于等于7个工艺路线的
		// 	printData.forEach(r => {
		// 		if (r.table.length >= 7)
		// 			printDataGreaterThanOREqual7.push(r);
		// 		else
		// 			printDataLessThan7.push(r);
		// 	});
		// 	if (printDataLessThan7.length > 0)
		// 		await goPrint(templateName, printType, printDataLessThan7);
		// 	if (printDataGreaterThanOREqual7.length > 0)
		// 		await goPrint('物流追踪卡-工艺大于等于7个则走此模版', printType, printDataGreaterThanOREqual7);
				
		// 		console.log(printDataLessThan7);
		// 		console.log(printDataGreaterThanOREqual7);
		// }
		// else
		// 	await goPrint(templateName, printType, printData);
	}

	//获取打印的数据
	const getPrintData = async (templateName : any, queryData : []) => {
		let url = `api/common/printDataLoad/${templateName}`;
		var response = await request({ url: url, method: 'post', data: queryData.map(r => r.id) });
		return response.data.result;
	};

	// 打开弹窗  printType :  0:打印预览后打印   1：直接打印
	const goPrint = async (templateName : string, printType : number, printData : any) => {

		let url = `api/common/printTemplate/${templateName}`;
		var res = await request({ url: url, method: 'get' });

		//打印预览
		if (printType == 0) {
			printShow(res.data.result.template, printData);
		} else if (printType == 1) { //直接打印
			printGO(res.data.result.template, printData);
		}
	};

	//直接打印，需要看打印机是否连接
	const printGO = (template : string, showData : any) => {
		//表示已连接打印机成功
		if (hiprint.hiwebSocket.opened == false) {
			// 注意：连接是异步的
			console.log('打印机已连接!')
		}
		else {
			// 重新连接客户端  当然你可以去连接局域网内的客户端
			//此客户端就是安装的hiprint_win_x64-1.0.6.exe ，默认就是 17521
			hiprint.hiwebSocket.setHost("http://localhost:17521");
			// 这是异步的,刷新打印机列表
			hiprint.refreshPrinterList((list) => {
				console.log('refreshPrinterList')
				console.log(list)
			});
		}
		showData = dataHandler(template, showData);
		// 批量直接打印
		hiprintTemplate.value.print2(showData);

		// // 发送任务到打印机成功
		hiprintTemplate.value.on('printSuccess', function (e) {

		});
		// // 发送任务到打印机失败
		// hiprintTemplate.on('printError', function (e) {})
	}

	//打印预览无需关注是否连接打印机
	const printShow = async (template : string, showData : any) => {
		showData = dataHandler(template, showData);
		hiprintTemplate.value.print(
			showData,
			{},
			{
				callback: () => {
					// state.waitShowPrinter = false;
				},
			}
		);
	};
	//数据处理
	const dataHandler = (template : string, showData : any) => {
		if (showData != null && showData.length > 0) {
			showData.forEach(r => {
				if (r.table != null && r.table != undefined && r.table.length > 0) {
					let tableArray = [];
					r.table.forEach(tab => {
						tableArray.push({
							NAME: tab.name,
							SL: tab.sl,
							DJ: tab.dj,
							GG: tab.gg,
							TM: tab.tm,
							JE: tab.je,
							AA: tab.aa,
							BB: tab.bb,
							CC: tab.cc,
							DD: tab.dd
						});
					});
					r.table = [];
					r.table = tableArray;
				}
				if (r.table2 != null && r.table2 != undefined && r.table2.length > 0) {
					let tableArray2 = [];
					r.table2.forEach(tab => {
						tableArray2.push({
							NAME: tab.name,
							SL: tab.sl,
							DJ: tab.dj,
							GG: tab.gg,
							TM: tab.tm,
							JE: tab.je,
							AA: tab.aa,
							BB: tab.bb,
							CC: tab.cc,
							DD: tab.dd
						});
					});
					r.table2 = [];
					r.table2 = tableArray2;
				}
			});
		}
		hiprintTemplate.value = new hiprint.PrintTemplate({
			template: JSON.parse(template)
		});
		return showData;
	}



	// // 提交
	// const submit = async () => {

	// 	let url=`api/mO_PO_WorkOrderOperation/SplitBatch/${mianPageData.value.id}/${splitCount}`;
	// 	var res = await request({url: url,method: 'get'});

	// };

	//将属性或者函数暴露给父组件
	defineExpose({ commentClick });
</script>